// Fill the DB with example data on startup

import { Meteor } from 'meteor/meteor';
import { Links } from '../../api/links/links.js';
import { Teams } from '../../api/teams/teams.js';
import { Arrangements } from '../../api/arrangements/arrangements.js';
import { Areas } from '../../api/areas/areas.js';
import { Roles } from '../../api/roles/roles.js';

Meteor.startup(() => {
  // if the Links collection is empty
  if (Links.find().count() === 0) {
    const data = [
      {
        title: 'Do the Tutorial',
        url: 'https://www.meteor.com/try',
        createdAt: new Date(),
      },
      {
        title: 'Follow the Guide',
        url: 'http://guide.meteor.com',
        createdAt: new Date(),
      },
      {
        title: 'Read the Docs',
        url: 'https://docs.meteor.com',
        createdAt: new Date(),
      },
      {
        title: 'Discussions',
        url: 'https://forums.meteor.com',
        createdAt: new Date(),
      },
    ];

    data.forEach(link => Links.insert(link));
  }
  if (Teams.find().count() == 0) {
    let teams = [
      {
        name: "LOD制作团队",
        address: "深圳南山",
        slogan: "优秀是一种习惯",
        actionCode: "go.stronger",
        createdAt: new Date(),
      },
      {
        name: "神风团队",
        address: "成都沙湾路",
        slogan: "优秀是一种习惯",
        actionCode: "envy.angel",
        createdAt: new Date(),
      },
    ];
    teams.forEach(team => Teams.insert(team));
  }
  let angel_team = Teams.findOne({actionCode: "envy.angel"});
  let stronger_team = Teams.findOne({actionCode: "go.stronger"});
  if (Areas.find().count() == 0) {
    let areas = [
      {
        orderId: "A区",
        title: "柜台区",
        description: "柜台区主要用于排队取，用于柜台服务",
        createdAt: new Date(),
        teamId: angel_team._id
      },
      {
        orderId: "B区",
        title: "汽车销售区",
        description: "汽车销售区主要用于销售人员在场的销售工作",
        createdAt: new Date(),
        teamId: angel_team._id
      },
      {
        orderId: "c区",
        title: "休息区",
        description: "吃饭，休息，聊天，small talk的区域",
        createdAt: new Date(),
        teamId: angel_team._id
      },
      {
        orderId: "S区",
        title: "冥想区",
        description: "Simon 大人在这里思考管理哲学， 哇哈哈哈哈",
        createdAt: new Date(),
        teamId: stronger_team._id
      }
    ];
    areas.forEach(area => Areas.insert(area));
  }
  if (Roles.find().count()==0) {
    let roles = [
      {
        name: "instructor",
        weight: "100"
      },
      {
        name: "superAdmin",
        weight: "0"
      },
      {
        name: "employee",
        weight: '1000'
      },
      {
        name: "partner",
        weight: "200"
      }
    ];
    roles.forEach(role => Roles.insert(role));
  }
  let instructor_role = Roles.findOne({name: "instructor"});
  let employee_role = Roles.findOne({name: "employee"});
  let partner_role = Roles.findOne({name: "partner"});
  if (Meteor.users.find().count() == 0) {
    console.log('user empty');
    let users = [
      {
      username: "fengxiaoqin",
      password: "fengxiaoqin2017",
    },
    {
      username: 'lilei',
      password: 'lilei2017',
    },
    {
      username: 'hanmeimei',
      password: 'hanmeimei2017',
    },
    {
      username: "wanghaitao",
      password: "wanghaitao2017",
    },{
      username: "baibo",
      password: 'baibo2017'
    },
    {
      username: "simonto",
      password: "xsq@519"
    }
  ];
  let user_infos = [
    {
      realname: "冯小琴",
      birthday: new Date(),
      age: 18,
      sex: "female",
      isArranged: false,
      roles: [
        instructor_role
      ],
      teamId: angel_team._id
    },
    {
      realname: "李雷",
      birthday: new Date(),
      age: 18,
      sex: "male",
      isArranged: false,
      roles: [
        employee_role
      ],
      teamId: angel_team._id
    },
    {
      realname: "韩梅梅",
      birthday: new Date(),
      age: 18,
      sex: "female",
      isArranged: false,
      roles: [
        employee_role
      ],
      teamId: angel_team._id
    },
    {
      realname: "王海涛",
      birthday: new Date(),
      age: 18,
      sex: "male",
      isArranged: false,
      roles: [
        employee_role
      ],
      teamId: angel_team._id
    },
    {
      realname: "白波",
      birthday: new Date(),
      age: 18,
      sex: "male",
      isArranged: false,
      roles: [
        employee_role
      ],
      teamId: angel_team._id
    },
    {
      realname: "Simon",
      birthday: new Date(),
      age: 18,
      sex: "male",
      isArranged: false,
      roles: [
        partner_role
      ],
      teamId: stronger_team._id
    },
  ];
  for (var i = 0; i < users.length; i++) {
    let user_id = Accounts.createUser(users[i]);
    console.log(user_id);
    Meteor.users.update(user_id, {
      $set: user_infos[i]
    });
  }
  }

});
